package com.tingyuge.mapper;

import com.tingyuge.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;

//@CacheNamespace(implementation = PerpetualCache.class)
//@CacheNamespace(implementation =RedisCache.class  )
public interface IUserMapper {
    //用户和订单所有
    List<User> findAll();

    List<User> findAllUserAndRole();

    @Insert("insert into user(id,username) values( #{id},#{username}) ")
    void addUser(User user);

    @Update("update user set username =#{username} where id =#{id}")
    int updateUser(User user);

    @Select("select * from user")
    List<User> selectUser();

    @Delete("delete from user where id =#{id}")
    void deleteUser(Integer id);

    @Select("select * from user where id = #{id}")
//    @Options(useCache = false)
    User findUserById(Integer id);

    //注解一对多
    @Select("select * from user")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "orderList",column = "id",
                    javaType = List.class,
                    many = @Many(select ="com.tingyuge.mapper.IOrderMapper.findByUid"))
    })
    List<User> findAllByAnnotation();
    //注解多对多
    @Select("select * from user")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "roleList",column = "id",
                    javaType = List.class,
                    many = @Many(select ="com.tingyuge.mapper.IRoleMapper.findRoleByUid"))
    })
    List<User> findRoleAndUserByAnnotation();
}
